<?php
namespace Admin\Controller;
use Org\Util\Date;

use Think\Controller;
class ArticleController extends BaseController {
    public function NewList(){
        $article =M('article');
       
       //获得查询条件
       $search=$_REQUEST["search"]?$_REQUEST["search"]:"";  
       $starttime=$_REQUEST["starttime"]?$_REQUEST["starttime"]:"";
       $endtime=$_REQUEST["endtime"]?$_REQUEST["endtime"]:"";
       $status=$_REQUEST["status"]?$_REQUEST["status"]:"";
       if($status==0)$status="";
       
       $column=$_REQUEST["column"]?$_REQUEST["column"]:"";
       
       
       $condition["article_status"]=array("gt",0);
       
       if($search){
       		$where["article_content"]=array("like","%$search%");
       		$where["article_name"]=array("like","%$search%");
       		$where["article_remark"]=array("like","%$search%");
			$where['_logic'] = 'or';
			$condition['_complex'] = $where;
       }
       if($starttime){
       		$condition["created_time"]=array("EGT","$starttime");
       }
	   if($endtime){
	   		$condition["created_time"]=array("ELT","$endtime");
	   }
	   if($status){
	   		$condition["article_status"]=array("eq","$status");
	   }
	   if($column){
	   		if($column==1){
	   			$condition["article_column"]=array("in","6,7,8,9,10,11,12,13,14");
	   		}elseif($column==2){
	   			$condition["article_column"]=array("in","15,16");
	   		}elseif($column==3){
	   			$condition["article_column"]=array("in","17,18");
	   		}elseif($column==4){
	   			$condition["article_column"]=array("in","19,20,21");
	   		}else{
	   			$condition["article_column"]=array("eq","$column");
	   		}
	   	}
	   
	    import('ORG.Util.Page');// 导入分页类
		$pre=12;//每页多少条
	    $count      = $article->where($condition)->count();// 查询满足要求的总记录数 $map表示查询条件
	    $Page       = new \Think\Page($count,$pre);// 实例化分页类 传入总记录数
	    
    	// 获取查询参数
   if($search){
    	$map['search'] = $search;
    	}
	    
    if($starttime){
    	$map['starttime'] = $starttime;
    	}
	    
    if($endtime){
    	$map['endtime'] = $endtime;
    	}
	    
    if($status){
    	$map['status'] = $status;
    	}
	   
    if($column){
    	$map['column'] = $column;
    	}
	    
	    
	    foreach($map as $key=>$val) {   
	    	$Page->parameter[$key]   =   urlencode($val); 
	    }

	    
	    $show       = $Page->show();// 分页显示输出
//	    var_dump($Page);exit;
	    // 进行分页数据查询
	    $list = $article->where($condition)->order('article_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
	    $this->assign('article_list',$list);// 赋值数据集
	   
	    $this->assign('page',$show);// 赋值分页输出


	   $this->assign('search', $search);
	   $this->assign('starttime', $starttime);
	   $this->assign('endtime', $endtime);
	   $this->assign('status', $status);
	   $this->assign('column', $column);
	   
    	
	   $this->display("new_list");
    }
    
    public function AnnexList(){
    	$file = M('file');
    	

    	import('ORG.Util.Page');// 导入分页类
		$pre=12;//每页多少条

	    $count      = $file->count();// 查询满足要求的总记录数 $map表示查询条件

	    
	    $Page       = new \Think\Page($count,$pre);// 实例化分页类 传入总记录数
   
	    
    	$show       = $Page->show();// 分页显示输出

	    // 进行分页数据查询

	    $list = $file->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
	    
	    $this->assign('annex_list',$list);// 赋值数据集
	   
	    $this->assign('page',$show);// 赋值分页输出
    	
    	
    	$this->display("annex_list");
    }
    
    public function annexdelete(){
    		$id=$_POST["radio_del"];
    		$file = M('file');
    		
    		$fileone=$file->where("id=$id")->find();
    		//有此记录
    		if($fileone){
    			$file_name=$fileone["file_name"];
    			$php_path = dirname(__FILE__) . '/';
				$file_dir=$php_path."../../../Public/upload/file/annex";
				$file_dir = $file_dir."/";
				if(file_exists($file_dir.$file_name))   {   //检查文件是否存在  
			 	 	$deletefile_result = @unlink ($file_dir.$file_name);
				}  
    			$result=$file->where("id=$id")->delete();
    		}else{
    			$this->success('非法连接！', U('Admin/Article/AnnexList', '', false, true));
    		
    		}
    	
	    	
    		if($result !== false  ){
				 $this->success("附件删除成功 ！", U('Admin/Article/AnnexList', '', false, true));
			}else{
				 $this->success('附件删除失败！', U('Admin/Article/AnnexList', '', false, true));
			}
    	
    }
    
	
    public function NewAdd(){
    	
    	$this->assign('isedit', 0);
		$this->display("new_add");
    }
    
	public function NewEdit(){
		$id=$_GET["id"];
		$article = M('article')->where("article_id=$id")->find();
		
		$this->assign('isedit', 1);
		$this->assign('article', $article);
		$this->display("new_add");
    }
    
    public function addarticle(){
	    $htmlData = '';
	    $article = M('article');
	    $data=$article->create();
		if (!empty($_POST['content'])) {
			if (get_magic_quotes_gpc()) {
				$htmlData = stripslashes($_POST['content']);
			} else {
				$htmlData = $_POST['content'];
			}
		}
		
		$data["article_content"]=$htmlData;
		//获得标题
		$data["article_name"]=$_POST['article_name'];
		//获得分类
		$data["article_column"]=$_POST['article_column'];
		$data["article_remark"]=$_POST['article_remark'];
		
		
		if($_POST['isedit']==1){//编辑状态
			$id=$_POST['article_id'];
			$result=$article->where("article_id=$id")->save($data);
	        
			if($result !== false){
		        $this->success('文章更新成功！', U('Admin/Article/NewList', '', false, true));
		    }else{
		        $this->error("文章更新失败");
		    }
			
		}elseif($_POST['isedit']==0){//新建状态
			$data["created_time"]=Date("Y-m-d H:i:s");
			$result=$article->add($data);
	        
			if($result !== false){
		        $this->success('文章新增成功！', U('Admin/Article/NewList', '', false, true));
		    }else{
		        $this->error("文章新增失败");
		    }
			
		}
		
    }
    
    public function Verify(){
    	
    	if($_POST["hidden_verify"]!=""){
    		$arr_verify=$_POST["hidden_verify"];
    		$arr_verify=substr($arr_verify,0,strlen($arr_verify)-1); 
    		$arr_verify=explode("_",$arr_verify);
    		
    		$n=0;
    		$article = M('article');
    		foreach($arr_verify as $a){
    			//拿到了文章ID
    			
    			$data=$article->create();
    			$data["article_status"]=2;
    			$result=$article->where("article_id=$a")->save($data);
    			
	    		if($result !== false){
			        $n++;
			    }
    		}
	    	
	    	
	    	$this->success("文章审核成功  $n 篇！", U('Admin/Article/NewList', '', false, true));
    	}else{
    		$this->success('没有勾选审核文章！', U('Admin/Article/NewList', '', false, true));
    	}
    	
    }
    
	public function isverify(){
		$isverify=$_SESSION['power'];
		if($isverify==4){
			return true;
		}else{
			return false;
		}
	
	    }
    
    public function Delete(){
    	
    	if($_POST["radio_del"]!=""){
    		$id=$_POST["radio_del"];
    		$article = M('article');
    		$data["article_status"]=0;
	    	$result=$article->where("article_id=$id")->save($data);
	    	
    		if($result !== false){
				 $this->success("文章删除成功 ！", U('Admin/Article/NewList', '', false, true));
			}else{
				 $this->success('没有勾选删除文章！', U('Admin/Article/NewList', '', false, true));
			}
    	}else{
	    	if($_POST["hidden_delete"]!=""){
	    		$arr_delete=$_POST["hidden_delete"];
	    		$arr_delete=substr($arr_delete,0,strlen($arr_delete)-1); 
	    		$arr_delete=explode("_",$arr_delete);
	    		
	    		$n=0;
	    		$article = M('article');
	    		foreach($arr_delete as $a){
	    			//拿到了文章ID
	    			
	    			$data=$article->create();
	    			$data["article_status"]=0;
	    			$result=$article->where("article_id=$a")->save($data);
	    			
		    		if($result !== false){
				        $n++;
				    }
	    		}
		    	
		    	
		    	$this->success("文章删除成功  $n 篇！", U('Admin/Article/NewList', '', false, true));
	    	}else{
	    		$this->success('没有勾选删除文章！', U('Admin/Article/NewList', '', false, true));
	    	}
    	}
    	
    }
    
    public function Uploadfile(){
		

   		$id=$_POST["upload_input"];
	
		
		//保存表单数据 包括附件数据
		$file = M("file"); // 实例化User对象
		$data=$file->create(); // 创建数据对象
	 	import('ORG.Net.UploadFile');
	 	
		$upload = new \Think\Upload();// 实例化上传类
		
		$upload->maxSize  = 100145728 ;// 设置附件上传大小100M
		//$upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');//设置附件上传类型
		$upload->rootPath = './Public/upload/file';
		
		$savepath="annex";
		$upload->savePath =  "/$savepath/";// 设置附件上传目录
		
		$upload->autoSub = false;
		//$upload->subType = 'date';  //子目录创建方式，默认为hash，可以设置为hash或者date 
        //$upload->dateFormat = 'Ym';
		
//		$upload->thumbRemoveOrigin = true;
//		//设置需要生成缩略图，仅对图像文件有效 
//        $upload->thumb = true;
//        //设置需要生成缩略图的文件后缀 
//        $upload->thumbPrefix = 'm_,s_,t_,i_';//生产4张缩略图 
//        //设置缩略图最大宽度 
//        $upload->thumbMaxWidth = '400,100,500,600';
//        //设置缩略图最大高度 
//        $upload->thumbMaxHeight = '400,100,200,420';    
//        $upload->thumbPath = './Public/upload/'.'pic';
		

		$info   =   $upload->upload();
		
		
	    if(!$info) {// 上传错误提示错误信息
	        $this->error($upload->getError());
	    }else{// 上传成功
	        //$this->success('上传成功！');
	        
	        //修改数据库
	        $data["file_name"] = $info['file']['savename'];
	    	
	        //是否有缩略图	
			//if(file_exists("./Public/Uploads/m_".$date['F_FilePath'])){
			//	$date["F_FileIsMin"] = 1;
			//}
			
	        $data["created_time"] = Date("Y-m-d H:i:s");
	        $data["article_id"] = $id;
	       
	        $result=$file->add($data);
	        
			if($result !== false){
		        $this->success("上传附件成功！", U('Admin/Article/NewList', '', false, true));
		    }else{
		        $this->error("上传失败！", U('Admin/Article/NewList', '', false, true));
		    }
	    }




    	
    }
    
    
    
}